package com.yanze.schoolinformation.viewModule.gank;

import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.model.bean.GankDataBean;
import com.yanze.schoolinformation.model.http.DeforObserver;
import com.yanze.schoolinformation.model.http.reqo.DataReqo;
import com.yanze.schoolinformation.view.information.gank.BigAndroidFragment;

import io.reactivex.disposables.Disposable;

/**
 * Created by MrZheng on 2018/4/27.
 */

public class BigAndroidViewModel {

    private BigAndroidNavigator mNavigator;
    private BigAndroidFragment mFragment;
    private String mType = "Android";
    private int mPage = 1;

    public BigAndroidViewModel(BigAndroidFragment mFragment) {
        this.mFragment = mFragment;
    }

    public void setNavigator(BigAndroidNavigator navigator) {
        this.mNavigator = navigator;
    }

    public int getPage() {
        return mPage;
    }

    public void setPage(int mPage) {
        this.mPage = mPage;
    }

    public void onDestroy() {
        mNavigator = null;
    }

    public void loadAndroidData() {
        DataReqo.getInstant().getGankIoData(C.url.API_GANKIO, mType, C.count.count, mPage)
                .subscribe(new DeforObserver<GankDataBean>() {
                    @Override
                    public void _onSubscribe(Disposable disposable) {
                        mFragment.addDisposable(disposable);
                    }

                    @Override
                    public void _onNext(GankDataBean entity) {
                        mNavigator.showLoadSuccessView();
                        if (mPage == 1) {
                            if (entity != null
                                    && entity.getResults() != null
                                    && entity.getResults().size() > 0) {
                                mNavigator.showAdapterView(entity);
                            } else {
                                mNavigator.showLoadFailedView();
                            }
                        } else {
                            if (entity != null
                                    && entity.getResults() != null
                                    && entity.getResults().size() > 0) {
                                mNavigator.refreshAdapter(entity);
                            } else {
                                mNavigator.showListNoMoreLoading();
                            }
                        }
                    }

                    @Override
                    public void _onError(String msg) {
                        mNavigator.showLoadFailedView();
                        if (mPage > 1) {
                            mPage--;
                        }
                    }
                });
    }
}
